﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNQXYJ.Model;
using HNQXYJ.BLL;

namespace HNQXYJ.HN_City
{
    public partial class CountyMgr : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // ViewState["parentCode"]= this.city.Value;
                CountyDataBind(1);
               
            }
            
        }
        //前一页
        protected void PrePage_Click(object sender, EventArgs e)
        {
            int currPage = Convert.ToInt32(this.LbPageNumber.Text);
            currPage--;
            if (this.btnNextPage.Enabled == false)
            {
                this.btnNextPage.Enabled = true;
            }
            if (currPage < 1)
            {
                currPage = 1;
                this.btnPrePage.Enabled = false;
            }
            
            
            CountyDataBind(currPage);

        }
        //后一页
        protected void NextPage_Click(object sender, EventArgs e)
        {
            int currPage = Convert.ToInt32(this.LbPageNumber.Text);
            int pageCount = Convert.ToInt32(this.LbPageCount.Text);
            currPage++;
            if (this.btnPrePage.Enabled == false)
            {
                this.btnPrePage.Enabled = true;
            }
            
            if (currPage > pageCount)
            {
                currPage = pageCount;
                this.btnNextPage.Enabled = false;
            }
            
            CountyDataBind(currPage);

        }
        //跳转页
        protected void BtnGo_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                int pageCount = Convert.ToInt32(this.LbPageCount.Text);
                int pageIndex = Convert.ToInt32(this.txtPageNumber.Text.Trim());
                if (pageIndex > pageCount)
                {
                    pageIndex = pageCount;
                    

                }
                if (pageIndex < 1)
                {
                    pageIndex = 1;
                   
                }
                CountyDataBind(pageIndex);
            }
        }
        //数据绑定
        private void CountyDataBind(int pageIndex)
        {
            
            int rowCount = 0;
            int pageCount = 0;
            string parentCode = this.HidCity.Value;// this.city.Value;
            List<HCounty> list = new CountyBLL().GetCountyPagedList(pageIndex, 2, parentCode, out rowCount, out pageCount);
            if (list != null)
            {
                this.RpCounty.DataSource = list;
                this.RpCounty.DataBind();
                this.LbPageCount.Text = pageCount.ToString();
                this.LbPageNumber.Text = pageIndex.ToString();
                this.txtPageNumber.Text = pageIndex.ToString();
                
            }
            else
            {
                this.RpCounty.DataSource = null;
                this.RpCounty.DataBind();
                this.LbPageCount.Text = "0";
                this.LbPageNumber.Text = "0";
                this.txtPageNumber.Text = "";
                this.btnPrePage.Enabled = false;
                this.btnNextPage.Enabled = false;
                this.BtnGo.Enabled = false;
            }
            
            
        }

        //搜索
        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            this.btnPrePage.Enabled = true;
            this.btnNextPage.Enabled = true;
            this.BtnGo.Enabled = true;
            CountyDataBind(1);
          

        }
    }
}